<?php

/*
 * Yanis Framework, Yet ANother Internet Service for web and mobile technologists
 * @copyright Copyright (C) 2011 ch@rlesen. All rights reserved.
 * @license GNU/GPL
 * @author Charles EDOU NZE <charles at charlesen.fr>
 */

/**
 * Class Page
 *
 */
class Page {

    /**
     * @var Object The Registry object 
     */
    private $registry;

    /**
     * @var Object TemplateInfos Class object 
     */
    private $_tplinfos;

    /**
     * @var Object Config Class object 
     */
    private $_config;

    /**
     * @var String Current page Title 
     */
    private $_title = '';

    /**
     * @var type Array The page Header
     */
    private $_header = array();

    /**
     * @var String Page content 
     */
    private $_content = '';

    /**
     * @var String 
     */
    private $_generator = '<!-- Generated by Yanis Framework, Alpha 1.0 version -->';

    /**
     * @var Array Template tags
     */
    private $_tags = '';

    /**
     * @var String 
     */
    private $_basename = '';

    /**
     * Build the registry object
     * @param Registry $registry 
     */
    public function __construct(Registry $registry) {
        require_once YANIS_PATH . 'config.php';
        $this->_config = new YConfig();
        require_once YANIS_PATH . 'templates/' . $this->_config->templateName . '/templateInfos.php';
        $this->_tplinfos = new TemplateInfos();

        $this->setBaseName();
        $this->registry = $registry;
    }

    /**
     * Get the page title from page
     * @return String 
     */
    public function getTitle() {
        return $this->_title;
    }

    /**
     * Set the page title
     * @param String $title 
     * @return void
     */
    public function setTitle($title) {
        $this->_title = $title . '-' . $this->_tplinfos->sitename;
    }

    /**
     * @param Array $new_header 
     */
    public function setHeader($new_header) {
        $header = array(
            'title' => $this->getTitle(),
            'jquery' => '<script type="text/javascript" src="' . YANIS_PATH . 'libraries/js/jquery.js' . '"></script>',
            'jqm' => '<script type="text/javascript" src="' . YANIS_PATH . 'libraries/js/jqm.min.js' . '"></script>',
            'modernizr' => '<script type="text/javascript" src="' . YANIS_PATH . 'libraries/js/modernizr.min.js' . '"></script>',
        );
        array_push($header, $this->_tplinfos->metadata);

        $this->_header = array_push($this->getHeader(), $new_header);
    }

    /**
     * @return Array Page Header 
     */
    public function getHeader() {
        return $this->_header;
    }

    /**
     * Set the page content
     * @param String $content 
     * @return void
     */
    public function setContent($content) {
        $this->_content = $content;
    }

    /**
     * Get the page content
     * @return String 
     */
    public function getContent() {
        return $this->_content;
    }

    public function setGenerator() {
        $numargs = func_num_args();
        // When passing a argument, such a new generator name
        if ($numargs == 1) {
            $this->_generator = func_get_arg(0);
        }
    }

    /**
     * @return String The site generator 
     */
    public function getGenerator() {
        return $this->_generator;
    }

    public function setBaseName() {
        $basename = strtolower(basename(YANIS_PATH));
        if (strpos($_SERVER['SERVER_NAME'], $basename) !== false) {
            $basename = '';
        } else {
            $basename .='/';
        }
        
        $this->_basename = 'http://' . $_SERVER['SERVER_NAME'] . '/' . $basename . 'templates/' . $this->_config->templateName . '/';
    }

    public function getBaseName() {
        return $this->_basename;
    }

    /**
     * Get the params of the current page
     * @param String $param
     * @return String 
     */
    public function getParam($param) {
        return (isset($_GET[$param])) ? $_GET[$param] : '';
    }

    /**
     * @param String $param
     * @return String the url view
     */
    public function getUrlPath($param) {
        require_once YANIS_PATH . 'config.php';
        require_once YANIS_PATH . 'libraries/yanis/mobile/mobile_detect.php';

        $config = new YConfig();
        $detect = new Mobile_Detect();
        $urlView = '';
        $path = '/';
        if ($this->getParam($param) !== '') {
            $path .= 'views/' . $this->getParam($param) . '.php';
        } else {
            $path .= 'index.php';
        }
        // Detect mobile devices
        if ($detect->isMobile()) {
            $urlView = YANIS_PATH . 'templates/mobile' . $path;
        } else {
            $urlView = YANIS_PATH . 'templates/' . $config->templateName . $path;
        }

        return $urlView;
    }

    /** Put a Javacript file (using absolute path) in the template page Header
     * @param type $absoluteScriptPath 
     */
    public function loadJS($absoluteScriptPath) {
        $js = '<script type="text/javascript" src="' . $absoluteScriptPath . '"></script>';
        $this->setHeader($js);
    }

}

?>
